import * as CryptoJS from "crypto-js";
import { compareSync, hashSync } from "bcryptjs";
// console.log(CryptoJS);

const KEY = CryptoJS.enc.Utf8.parse("mySoft2025RpsApp");
const IV = CryptoJS.enc.Utf8.parse("mySoft2025RpsApp");

export class CryptoUtils {
  // 加密
  static encrypt(text: string): string {
    const srcs = CryptoJS.enc.Utf8.parse(text);
    const encrypted = CryptoJS.DES.encrypt(srcs, KEY, { iv: IV, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Iso10126 });
    return encrypted.ciphertext.toString().toUpperCase();
  }
  // 解密
  static decrypt(text: string): string {
    const encryptedHexStr = CryptoJS.enc.Hex.parse(text);
    const str = CryptoJS.enc.Base64.stringify(encryptedHexStr);
    const decrypt = CryptoJS.DES.decrypt(str, KEY, {
      iv: IV,
      mode: CryptoJS.mode.CBC,
      padding: CryptoJS.pad.Iso10126
    });
    const decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
    return decryptedStr.toString();
  }

  static compare(decryptPassword: string, password: string) {
    return compareSync(decryptPassword, password);
  }

  static hashSync(password: string) {
    return hashSync(password, 10);
  }
}
